define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'stock/info/index',
                    add_url: 'stock/info/add',
                    edit_url: 'stock/info/edit',
                    del_url: 'stock/info/del',
                    multi_url: 'stock/info/multi',
                    table: 'stock_info',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'third_id', title: __('Third_id')},
                        {field: 'waybill_id', title: __('Waybill_id')},
                        {field: 'qrcode', title: __('Qrcode')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'consignee', title: __('Consignee')},
                        {field: 'consignee_address', title: __('Consignee_address')},
                        {field: 'consignee_mobile', title: __('Consignee_mobile')},
                        {field: 'consignee_node', title: __('Consignee_node')},
                        {field: 'consignor', title: __('Consignor')},
                        {field: 'consignor_address', title: __('Consignor_address')},
                        {field: 'consignor_node', title: __('Consignor_node')},
                        {field: 'consignor_mobile', title: __('Consignor_mobile')},
                        {field: 'consignor_time', title: __('Consignor_time'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'package_time', title: __('Package_time'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'last_time', title: __('Last_time'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'consignee_confirm', title: __('Consignee_confirm'), searchList: {"0":__('Consignee_confirm 0'),"1":__('Consignee_confirm 1')}, formatter: Table.api.formatter.normal},
                        {field: 'confirm_time', title: __('Confirm_time'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'confirm_info', title: __('Confirm_info')},
                        {field: 'fare', title: __('Fare'), operate:'BETWEEN'},
                        {field: 'payment_status', title: __('Payment_status'), formatter: Table.api.formatter.status},
                        {field: 'payment_method', title: __('Payment_method')},
                        {field: 'payment_time', title: __('Payment_time'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'payment_return_num', title: __('Payment_return_num')},
                        {field: 'payment_return_code', title: __('Payment_return_code')},
                        {field: 'amount', title: __('Amount')},
                        {field: 'weight', title: __('Weight'), operate:'BETWEEN'},
                        {field: 'cubage', title: __('Cubage'), operate:'BETWEEN'},
                        {field: 'fragile', title: __('Fragile')},
                        {field: 'remark', title: __('Remark')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            
            // 绑定TAB事件
            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    var filter = {};
                    if (value !== '') {
                        filter[field] = value;
                    }
                    params.filter = JSON.stringify(filter);
                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});